package com.dtsheep.elasticsearch.es.client.document;

import com.alibaba.fastjson.JSON;
import com.dtsheep.elasticsearch.es.client.ElasticsearchClientUtil;
import com.dtsheep.elasticsearch.po.User;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.common.xcontent.XContentType;

import java.io.IOException;

/**
 * InsertDocument
 *
 * <p>
 *     插入文档
 * </p>
 *
 * @author chuanbai
 * @date 2024/5/24
 */
public class InsertDocument {

    public static void main(String[] args) throws IOException {
        User user = new User("小明", 21, "男");
        String userJson = JSON.toJSONString(user);

        IndexResponse response = ElasticsearchClientUtil.getEsClient()
                .index(new IndexRequest()
                        .index("user")
                        .id("1")
                        .source(userJson, XContentType.JSON), RequestOptions.DEFAULT);
        //结果
        System.out.println(response.getResult());
    }
}
